package com.slz3.business.frame.aop;

import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;

@Aspect
@Component
public class HandlerAOP {

    /**
     * 受影响的方法
     */
    @Pointcut("execution(public * com.slz3.business.module.*.*.*Handler.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.err.println("aop around: ".concat(pjp.getSignature().getName()));
        Object obj = null;
        try {

            before(pjp);

            obj = pjp.proceed();

        } finally {

        }
        return obj;
    }

    private void before(ProceedingJoinPoint pjp) {

        initReqResInfo(pjp);

    }

    /**
     * @appoint {ModuleMenu}Handler 中方法必须有参数，并且第一个参数必须是 ServerRequest 类型。
     * @param pjp
     */
    private void initReqResInfo(ProceedingJoinPoint pjp) {

        ServerRequest req = ((ServerRequest) pjp.getArgs()[0]);

        ResInfo resInfo = RR.initReqResInfo(req);

        req.attributes().put(RR.RES_INFO, resInfo);

    }

}
